﻿/* Write a program that shows the binary representation of given 16-bit signed integer number (the C# type short). */

using System;

class BinaryOfShort
{
    static void Main()
    {
        short n;

        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.Write("Error!: Invalid user input. Please provide a valid integer number of type short (16 bit)\r\nValid value range is from {0} to {1}!", short.MinValue, short.MaxValue);
            else iProvided = true;
            Console.Write("Enter a number of type short: ");
        } while (short.TryParse(Console.ReadLine(), out n) == false);

        char[] bit = new char[16];
        short pos = 15;
        short i = 0;
        //go to each position and check with the bitwise operation below if the value is 0 or 1
        while (i < 16)
        {
            if ((n & (1 << i)) != 0)
            {
                bit[pos] = '1';
            }
            else
            {
                bit[pos] = '0';
            }
            pos--;
            i++;
        }
        string result = new string(bit);

        //output result
        Console.WriteLine("Number you entered: {0}\r\nPresented in binary: {1} ", n, result);
    }
}
